package com.sciai.utils;

import cn.com.sciai.aix.common.http.SciaiHttpResponseEntity;
import cn.com.sciai.aix.common.http.SciaiHttpResponseEntityBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 邮件服务
 * @author shelong
 * @version 1.0.0.0
 * <p>Title: EmailUtil </p >
 * Package: com.sciai.utils
 * Company: 沪渝研究院
 * Copyright: Copyright(c) AI+X
 * Date: 2023年04月18日 14:08
 */
@Slf4j
public class EmailUtil {

    /**
     * 邮箱合法性验证
     */
    private static final String CHECK_EMAIL = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\\\.[a-zA-Z0-9]+";
//    private static final String CHECK_EMAIL = "^[a-zA-Z0-9_\\-\\.]+@([a-zA-Z0-9_\\-]+\\.[a-zA-Z0-9]+)+$";

    public static SciaiHttpResponseEntity checkToEmail(String to) {
        if (StringUtils.isEmpty(to)) {
            return SciaiHttpResponseEntityBuilder.builder().error("500","请输入邮箱").build();
        }

        Pattern compile = Pattern.compile("^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))$",Pattern.CASE_INSENSITIVE);
        Matcher matcher = compile.matcher(to);
        boolean f = matcher.matches();
        log.info(String.valueOf(f));

        if (!f) {
            return SciaiHttpResponseEntityBuilder.builder().error("500","输入邮箱格式不合法").build();
        }
        return SciaiHttpResponseEntityBuilder.builder().ok("邮件校验成功").build();
    }

}
